/**
 * author wen
 * cunyu
 * email wjd_13@hotmail.com
 */
package com.cunyu.service;

import cn.tom.db.jdbc.simple.DBQuery;
import cn.tom.kit.StringUtil;
import com.cunyu.bean.Form;
import com.cunyu.bean.Muser;
import com.cunyu.bean.ReqData;
import com.cunyu.bean.RespData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 订单相关业务
 * @author win
 * 2019/04/29-10:49
 */
@Service
public class OrderService extends BaseService{

    private static final Logger LOGGER = LoggerFactory.getLogger(OrderService.class) ;

    public RespData<List<Map<String, Object>>> searchPage(ReqData reqData, Form form, Muser u){
        reqData.setOrder("desc");
        reqData.setSort("m.ordtime");
        /*String sql = "select m.id,m.uid,m.oid,m.ordtime::date,m.state,m.ptype,m.source," +
                " (select u.name from userinfo u where u.uid = m.uid )," +
                " (select s.tel from suser s where s.id = m.uid )" +
                " from myorder m " ;*/
        String sql = "select m.id,m.uid,m.oid,m.ordtime::date,m.state,m.ptype,m.source," +
                " u.name,s.tel,m.rprice from myorder m left join suser s on s.id = m.uid " +
                "left join userinfo u on u.uid = m.uid where 1=1 " ;
        DBQuery query = new DBQuery(sql );
        if(StringUtil.hasLength(form.getUname())){
            query.and(" u.name like ? ").setParams("%"+form.getUname()+"%") ;
        }
        if(StringUtil.hasLength(form.getTel())){
            query.and(" s.tel = ? ").setParams(Long.valueOf(form.getTel())) ;
        }
        if(StringUtil.hasLength(form.getState())){
            query.and(" m.state = ? ").setParams(Integer.valueOf(form.getState())) ;
        }
        return page(query.getSql(),reqData,query.getParams()) ;
    }

    /**
     * 用户申诉退单后后台确认，可为其退款/返还抢单券
     */
    @Transactional(rollbackFor = Exception.class)
    public int refund(Muser u, int id) {
        if (u.getRole() != 1)
            return -1;

        // 获取订单信息
        String info = "select * from myorder where id = ?";
        Map<String, Object> map = getMap(info, id);
        int state = (int) map.get("state");
        // 判断状态是否为 申请退款
        if (state != 2) {
            return -1;
        }
        // 修改订单状态为已退款
        String up = "update myorder set state = 3 where id = ? ";
        exec(up, id);
        //orderpool 状态设置
        int oid = (int) map.get("oid");
        String upOP = "update orderPool set state = 2 where id = ? ";
        exec(upOP, oid);
        // 为用户账户回退金额
        int ptype = (int) map.get("ptype");
        int uid = (int) map.get("uid");
        // 抢单券
        if (ptype == 0) {
            String basql = "update ucoupon set state = 0 where state = 1 and uid = ? and " +
                    " id = (select max(id) from ucoupon where uid = ? and state = 1 )";
            exec(basql, uid, uid);
        } else if (ptype == 1) {
            // 金币
            int g = (int) map.get("rprice");
            if (g != 0) {
                String aSql = "update account set money = money + ? where uid = ? ";
                exec(aSql, g, uid);
                String lSql = "insert into accountLog (money,type,uid,des,ctime) values (?,?,?,?,now())";
                insert(lSql, g, 0, uid, "成功退款，金币返还");
            }
        } else {
            throw new RuntimeException("unknow pay type " + ptype);
        }
        // 生成记录
        return 1;
    }

    // 拒绝退单，目前不给用户发送拒绝理由，不发消息。
    public int refuse(Muser u, int id) {
        if (u.getRole() != 1)
            return -1;
        // 修改订单状态
        String sql = "update myOrder set state = 4 where id = ? and state = 2" ;
        exec(sql,id) ;
        //修改申诉表状态
        return 1 ;
    }

    public int getTheSuserId(int id) {
        String sql = "select uid from myOrder where id = ? ";
        return getInt(sql,id) ;
    }

}
